﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BackgroundAnimationScript : MonoBehaviour {
    public SpriteRenderer SR;
    public List<Sprite> spriteList;
    public List<Sprite> secondeSpriteList;
    public float AnimationSpeed = 0.5f;

    private float lastUpdateTime = -100f;
    private int index;
    private bool isSecondAnimation;

    void Update()
    {
        if (Time.time - lastUpdateTime > AnimationSpeed)
        {

            index++;
            if (isSecondAnimation)
            {
                if (index >= secondeSpriteList.Count)
                {
                    index = 0;
                }
                SR.sprite = secondeSpriteList[index];
            }
            else
            {
                if (index >= spriteList.Count)
                {
                    index = 0;
                }
                SR.sprite = spriteList[index];
            }
            lastUpdateTime = Time.time;
        }
    }

    public void ChangeToSecond()
    {
        if (secondeSpriteList.Count > 0)
        {
            isSecondAnimation = true;
            index = -1;
        }
    }
}
